package com.fz.us.admin.company.dao.impl;

import com.fz.us.admin.company.entity.Admin;
import com.fz.us.admin.base.dao.impl.BaseDaoImpl;
import com.fz.us.admin.company.bean.EnumManage;
import com.fz.us.admin.company.dao.AdminDao;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * Dao实现类 - 管理员
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-25 。
 * ============================================================================
 */
@Repository
public class AdminDaoImpl extends BaseDaoImpl<Admin, String> implements AdminDao {

    public boolean isExistByUsername(String username) {
        Assert.hasText(username, "username 不能为空");

        String hql = "from "+ Admin.class.getName() +" model where lower(model.username) = lower(:username) ";
        Admin admin = (Admin) getSession().createQuery(hql).setParameter("username",username).uniqueResult();
        return (admin != null);
    }

    public boolean isExistByUsermobile(String usermobile) {
        Assert.hasText(usermobile, "usermobile 不能为空");

        String hql = "from "+ Admin.class.getName() +" model where lower(model.usermobile) = lower(:usermobile) ";
        Admin admin = (Admin) getSession().createQuery(hql).setParameter("usermobile", usermobile).uniqueResult();
        return (admin != null);
    }

    public boolean isExistByUseremail(String useremail) {
        Assert.hasText(useremail, "useremail 不能为空");
        String hql = "from "+ Admin.class.getName() +" model where lower(model.useremail) = lower(:useremail) ";
        Admin admin = (Admin) getSession().createQuery(hql).setParameter("useremail", useremail).uniqueResult();
        return (admin != null);
    }

    public Admin getAdminByAccount(String account) {
         Assert.hasText(account, "account 不能为空");
         String hql = "from "+ Admin.class.getName() +" model where model.username = :account or model.usermobile = :account or model.useremail = :account ";
         Admin admin = null;
         try {
             admin = (Admin) getSession().createQuery(hql).setParameter("account", account).uniqueResult();
         }catch (Exception e){
            e.printStackTrace();
         }
         return  admin;

    }

    public boolean isExistByOpenId(String openId) {
        Assert.hasText(openId, "openId 不能为空");

        String hql = "from "+ Admin.class.getName() +" model where lower(model.openId) = lower(:openId) ";
        Admin admin = (Admin) getSession().createQuery(hql).setParameter("openId", openId).uniqueResult();
        return (admin != null);
    }

    public Admin getAdminByOpenId(String openId) {
        Assert.hasText(openId, "openId 不能为空");

        String hql = "from "+ Admin.class.getName() +" model where model.openId = :openId";
        return (Admin) getSession().createQuery(hql).setParameter("openId", openId).uniqueResult();
    }

    public Admin getWeixinAdminByLastOpenId(String openId) {
        Assert.hasText(openId, "openId 不能为空");

        String hql = "from "+ Admin.class.getName() +" model where model.lastOpenId = :openId and model.accountEnum = :accountEnum ";
        return (Admin) getSession().createQuery(hql).setParameter("openId", openId).setParameter("accountEnum", EnumManage.AccountEnum.WEIXIN).uniqueResult();
    }

}
